package com.bsg.upm.check;

import java.text.MessageFormat;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;
import org.springframework.stereotype.Service;

import com.bsg.upm.constant.Messages;
import com.bsg.upm.entity.AreaEntity;
import com.bsg.upm.entity.BackupStorageEntity;
import com.bsg.upm.entity.SiteEntity;

/**
 * 区域业务处理检查
 * 
 * @author HCK
 *
 */
@Service
public class AreaCheck extends BaseCheck {

	/**
	 * 区域新增时检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	public CheckResult checkSave(Map<String, Object> paramMap) {
		// 非逻辑检查
		CheckResult chkRS = checkSaveNonLogic(paramMap);
		if (chkRS.isError()) {
			return chkRS;
		}

		// 逻辑检查
		chkRS = checkSaveLogic(paramMap);
		if (chkRS.isError()) {
			return chkRS;
		}
		return chkRS;
	}

	/**
	 * 区域编辑时检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	public CheckResult checkUpdate(Map<String, Object> paramMap) {
		// 非逻辑检查
		CheckResult chkRS = checkUpdateNonLogic(paramMap);
		if (chkRS.isError()) {
			return chkRS;
		}

		// 逻辑检查
		chkRS = checkUpdateLogic(paramMap);
		if (chkRS.isError()) {
			return chkRS;
		}
		return chkRS;
	}

	/**
	 * 区域启用时检查
	 * 
	 * @param area
	 *            区域对象
	 * @return CheckResult检查结果对象
	 */
	public CheckResult checkEnable(AreaEntity area) {
		CheckResult chkRS = new CheckResult();
		// 存在性检查
		if (area == null) {
			chkRS.setStatus(HttpStatus.SC_GONE);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NOT_EXIST, "区域"));
			return chkRS;
		}

		// 区域状态检查
		if (area.getEnabled()) {
			chkRS.setStatus(HttpStatus.SC_CONFLICT);
			chkRS.setErrorMsg(Messages.ENABLED_ALREADY);
			return chkRS;
		}
		return chkRS;
	}

	/**
	 * 区域停用时检查
	 * 
	 * @param area
	 *            区域对象
	 * @return CheckResult检查结果对象
	 */
	public CheckResult checkDisable(AreaEntity area) {
		CheckResult chkRS = new CheckResult();
		// 存在性检查
		if (area == null) {
			chkRS.setStatus(HttpStatus.SC_GONE);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NOT_EXIST, "区域"));
			return chkRS;
		}

		// 区域状态检查
		if (!area.getEnabled()) {
			chkRS.setStatus(HttpStatus.SC_CONFLICT);
			chkRS.setErrorMsg(Messages.DISABLED_ALREADY);
			return chkRS;
		}
		return chkRS;
	}

	/**
	 * 区域删除时检查
	 * 
	 * @param area
	 *            区域对象
	 * @return CheckResult检查结果对象
	 */
	public CheckResult checkRemove(AreaEntity area) {
		CheckResult chkRS = new CheckResult();
		// 存在性检查
		if (area == null) {
			chkRS.setStatus(HttpStatus.SC_GONE);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NOT_EXIST, "区域"));
			return chkRS;
		}

		// 区域状态检查
		if (area.getEnabled()) {
			chkRS.setStatus(HttpStatus.SC_CONFLICT);
			chkRS.setErrorMsg(MessageFormat.format(Messages.ENABLED_NOT_DELETE, "区域"));
			return chkRS;
		}

		// 区域关联资源检查
		if (clusterDao.countByAreaId(area.getId()) > 0) {
			chkRS.setStatus(HttpStatus.SC_CONFLICT);
			chkRS.setErrorMsg("该区域已关联集群，无法删除");
			return chkRS;
		}
		return chkRS;
	}

	/**
	 * 新增时非业务逻辑(非空和长度)检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	private CheckResult checkSaveNonLogic(Map<String, Object> paramMap) {
		CheckResult chkRS = new CheckResult();
		// 站点非空检查
		String siteId = (String) paramMap.get("site");
		if (StringUtils.isBlank(siteId)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "所属站点"));
			return chkRS;
		}

		// 区域名称非空和长度检查
		String name = (String) paramMap.get("name");
		if (StringUtils.isBlank(name)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "区域名称"));
			return chkRS;
		}
		if (StringUtils.trim(name).length() > 64) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.MAXLENGTH_NOT_ALLOWED, "区域名称", 64));
			return chkRS;
		}

		// 区域名描述长度检查
		if (paramMap.containsKey("description")) {
			// 描述长度检查
			String description = (String) paramMap.get("description");
			if (StringUtils.trim(description).length() > 256) {
				chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
				chkRS.setErrorMsg(MessageFormat.format(Messages.MAXLENGTH_NOT_ALLOWED, "描述", 256));
				return chkRS;
			}
		}

		// 备份存储非空检查
		String backupStorageId = (String) paramMap.get("backupStorage");
		if (StringUtils.isBlank(backupStorageId)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "备份存储"));
			return chkRS;
		}

		return chkRS;
	}

	/**
	 * 新增时业务逻辑检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	private CheckResult checkSaveLogic(Map<String, Object> paramMap) {
		CheckResult chkRS = new CheckResult();
		// 站点存在性检查
		String siteId = (String) paramMap.get("site");
		SiteEntity site = siteDao.get(siteId);
		if (site == null) {
			chkRS.setStatus(HttpStatus.SC_GONE);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NOT_EXIST, "所属站点"));
			return chkRS;
		}

		// 区域名称重复性检查
		String name = (String) paramMap.get("name");
		if (areaDao.countBySiteIdAndName(siteId, name) > 0) {
			chkRS.setStatus(HttpStatus.SC_CONFLICT);
			chkRS.setErrorMsg(MessageFormat.format(Messages.EXIST, "站点下区域名称"));
			return chkRS;
		}

		// 该站点下备份存储存在性检查
		String backupStorageId = (String) paramMap.get("backupStorage");
		BackupStorageEntity backupStorage = backupStorageDao.get(backupStorageId);
		if (backupStorage == null || !siteId.equals(backupStorage.getSite().getId())) {
			chkRS.setStatus(HttpStatus.SC_GONE);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NOT_EXIST, "站点下备份存储"));
			return chkRS;
		}

		// 备份存储状态检查
		if (!backupStorage.getEnabled()) {
			chkRS.setStatus(HttpStatus.SC_GONE);
			chkRS.setErrorMsg(MessageFormat.format(Messages.DISABLED_NOT_SELECT, "备份存储"));
			return chkRS;
		}

		return chkRS;
	}

	/**
	 * 编辑时非业务逻辑(非空和长度)检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	private CheckResult checkUpdateNonLogic(Map<String, Object> paramMap) {
		CheckResult chkRS = new CheckResult();
		// 区域非空检查
		String areaId = (String) paramMap.get("areaId");
		if (StringUtils.isBlank(areaId)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "区域编码"));
			return chkRS;
		}

		if (paramMap.containsKey("name")) {
			// 区域名称非空和长度检查
			String name = (String) paramMap.get("name");
			if (StringUtils.isBlank(name)) {
				chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
				chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "区域名称"));
				return chkRS;
			}
			if (StringUtils.trim(name).length() > 64) {
				chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
				chkRS.setErrorMsg(MessageFormat.format(Messages.MAXLENGTH_NOT_ALLOWED, "区域名称", 64));
				return chkRS;
			}
		}

		if (paramMap.containsKey("description")) {
			// 描述长度检查
			String description = (String) paramMap.get("description");
			if (StringUtils.trim(description).length() > 256) {
				chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
				chkRS.setErrorMsg(MessageFormat.format(Messages.MAXLENGTH_NOT_ALLOWED, "描述", 256));
				return chkRS;
			}
		}

		if (paramMap.containsKey("backupStorage")) {
			// 备份存储非空检查
			String backupStorageId = (String) paramMap.get("backupStorage");
			if (StringUtils.isBlank(backupStorageId)) {
				chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
				chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "备份存储"));
				return chkRS;
			}
		}

		return chkRS;
	}

	/**
	 * 编辑时业务逻辑检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	private CheckResult checkUpdateLogic(Map<String, Object> paramMap) {
		CheckResult chkRS = new CheckResult();
		// 区域存在性检查
		String areaId = (String) paramMap.get("areaId");
		AreaEntity area = areaDao.get(areaId);
		if (area == null) {
			chkRS.setStatus(HttpStatus.SC_GONE);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NOT_EXIST, "区域"));
			return chkRS;
		}

		// 区域状态检查
		if (area.getEnabled()) {
			chkRS.setStatus(HttpStatus.SC_CONFLICT);
			chkRS.setErrorMsg(MessageFormat.format(Messages.ENABLED_NOT_UPDATE, "区域"));
			return chkRS;
		}

		String siteId = area.getSite().getId();
		if (paramMap.containsKey("name")) {
			String areaName = (String) paramMap.get("name");
			if (!areaName.equals(area.getName())) {
				// 区域名称重复性检查
				if (areaDao.countBySiteIdAndName(siteId, areaName) > 0) {
					chkRS.setStatus(HttpStatus.SC_CONFLICT);
					chkRS.setErrorMsg(MessageFormat.format(Messages.EXIST, "站点下区域名称"));
					return chkRS;
				}
			}
		}

		if (paramMap.containsKey("backupStorage")) {
			String backupStorageId = (String) paramMap.get("backupStorage");
			if (!backupStorageId.equals(area.getBackupStorage().getId())) {
				// 备份存储关联资源检查
				if (hostDao.countInputByAreaId(areaId) > 0) {
					chkRS.setStatus(HttpStatus.SC_CONFLICT);
					chkRS.setErrorMsg("该备份存储已关联主机，不能修改");
					return chkRS;
				}

				// 该站点下备份存储存在性检查
				BackupStorageEntity backupStorage = backupStorageDao.get(backupStorageId);
				if (backupStorage == null || !siteId.equals(backupStorage.getSite().getId())) {
					chkRS.setStatus(HttpStatus.SC_GONE);
					chkRS.setErrorMsg(MessageFormat.format(Messages.NOT_EXIST, "站点下备份存储"));
					return chkRS;
				}

				// 备份存储状态检查
				if (!backupStorage.getEnabled()) {
					chkRS.setStatus(HttpStatus.SC_GONE);
					chkRS.setErrorMsg(MessageFormat.format(Messages.DISABLED_NOT_SELECT, "备份存储"));
					return chkRS;
				}
			}
		}
		return chkRS;
	}
}
